import Vue from 'vue'
import VueRouter from 'vue-router'
import lauya from '../laiyaView.vue'
Vue.use(VueRouter)

const routes = [
  {
    //一级路由
    path: '/',
    name: 'lauya',
    component: lauya,
    redirect:"/home",
    children:[
      {
        //二级路由
        //首页
        path: 'home',
        name: 'home',
        redirect:'/home/follow',
        component: () => import( '../views/HomeView.vue'),
        children:[
          //三级路由
          //关注
          {
            path: 'follow',
            name: 'follow',
            component: () => import( '../views/follow.vue')
          },
          {
            //笔记
            path: 'note',
            name: 'note',
            component: () => import( '../views/note.vue')
          },
          {
            //推荐
            path: 'recommend',
            name: 'recommend',
            component: () => import( '../views/recommend.vue')
          },
        ]
      },
      {
        //收藏
        //一级路由
        path: 'about',
        name: 'about',
        redirect:'/about/CaiCollection',
        component: () => import( '../views/AboutView.vue'),
        children:[
          {
            //二级路由
            //菜谱收藏
            path: 'CaiCollection',
            name: 'CaiCollection',
            component: () => import( '../views/CaiCollection.vue')
          },
          {
            //笔记收藏
            path: 'noteCollection',
            name: 'noteCollection',
            component: () => import( '../views/noteCollection.vue')
          }
        ]
      },
      {
        //用户中心
        path: 'user',
        name: 'user',
        component: () => import( '../views/userView.vue'),
        //代表路由拦截 
        beforeEnter: (to, from, next) => {
          //如果没有登录就跳转登录页面
          if(localStorage.getItem("user")){
            next()
          }else{
            next("/denglu")
          }
        }
      },
    ]
  },
  {
    //搜索页面
    path: '/serch',
    name: 'serch',
    component: () => import( '../views/serch.vue')
  },
  
  {
    //404页面
    //假如路由出错可以跳转404
    path:"*",
    component: () => import( '../views/Errone.vue')
  },
  {
    //登录页面
    path:"/denglu",
    component: () => import( '../views/denglu.vue')
  },
  {
    //菜谱详情
    path:"/Caidetails",
    component: () => import( '../views/Caidetails.vue')
  },
  
  {
    //笔记详情
    path:"/NoteDetail",
    component: () => import( '../views/NoteDetail.vue')
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
